﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BrainTechLLC;
using BrainTechLLC.Silverlight.Controls;
using System.Threading;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class AddThing 
    {
        protected Type _thingType;

        public AddThing()
        {
            InitializeComponent();
        }

        private void typeFilter_TypeChanged(object sender, ObjectEventArgs<Type> e)
        {
            _thingType = e.obj;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (_thingType == null)
            {
                MessageBox.Show("Select a type of thing to create.");
            }
            else
            {
                object o = TypeProvider.GetInstance().CreateInstanceFromType(_thingType);
                DataAccess.LastStoredId = 0;
                o.StoreThingAsync();
                DataAccess.StoreCompleted += new EventHandler<ObjectEventArgsNonRef<long>>(DataAccess_StoreCompleted);                
            }
        }

        void DataAccess_StoreCompleted(object sender, ObjectEventArgsNonRef<long> e)
        {
            DataAccess.StoreCompleted -= DataAccess_StoreCompleted;
            DataAccess.Client.ReadByIdAsync(DataAccess.AssignedUniqueId, DataAccess.LastStoredId);
        }
        
    }
}
